//
//  GHXingXingView.m
//  GodHorses
//
//  Created by coder on 2017/11/21.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "STTools_XingXIngView.h"
#define tagBase 10000
#define xingxing_selectedImage [UIImage imageNamed:@"星_收藏_标记"]
#define xingxing_nomalImage [UIImage imageNamed:@"星_收藏_标记 拷贝 4"]
@interface STTools_XingXIngView()
@property(nonatomic, strong) NSMutableArray                     *buttonArray;
@property(nonatomic, assign) NSInteger                          maxsNum;
@end
@implementation STTools_XingXIngView

- (instancetype)initWithFrame:(CGRect)frame maxsNum:(NSInteger)maxsNum{
    if (self == [super initWithFrame:frame]) {
        self.maxsNum = maxsNum;
        [self stoneobs_ui_configSubView];
    }
    return self;
}
#pragma mark --subView
- (void)stoneobs_ui_configSubView{
    CGFloat inset = 3;
    self.buttonArray = [NSMutableArray new];
    CGFloat height = self.height;
    for (int i = 0; i< self.maxsNum; i++) {
        STButton * stoneobs_temp_xingxingButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, height, height)
                                                              title:nil
                                                         titleColor:nil
                                                          titleFont:0
                                                       cornerRadius:0
                                                    backgroundColor:nil
                                                    backgroundImage:nil
                                                              image:xingxing_nomalImage];
        stoneobs_temp_xingxingButton.tag = i + tagBase;
        stoneobs_temp_xingxingButton.centerY = self.height / 2;
        stoneobs_temp_xingxingButton.left = height* i + inset * i;
        [stoneobs_temp_xingxingButton setImage:xingxing_selectedImage forState:UIControlStateSelected];
        stoneobs_temp_xingxingButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [stoneobs_temp_xingxingButton addTarget:self action:@selector(stoneobs_action_onSelectedstoneobs_temp_xingxingButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonArray addObject:stoneobs_temp_xingxingButton];
        [self addSubview:stoneobs_temp_xingxingButton];
        self.width = stoneobs_temp_xingxingButton.right;
    }
}
- (void)setSelcted_image:(UIImage *)selcted_image{
    _selcted_image = selcted_image;
    for (UIButton * btn in self.buttonArray) {
        [btn setImage:selcted_image forState:UIControlStateSelected];
    }
}
- (void)setNormal_image:(UIImage *)normal_image{
    _normal_image = normal_image;
    for (UIButton * btn in self.buttonArray) {
        [btn setImage:normal_image forState:UIControlStateNormal];
    }
}
#pragma mark --Action Method
- (void)stoneobs_action_onSelectedstoneobs_temp_xingxingButton:(UIButton*)sender{
    
    for (UIButton * button in self.buttonArray) {
        if (button.tag <= sender.tag ) {
            button.selected = YES;
        }else{
            button.selected = NO;
        }
    }
    self.chosedNum = sender.tag - tagBase + 1;
    if (self.itemActionHandle) {
        self.itemActionHandle(sender,self);
    }
}
- (void)stoneobs_fun_st_makeXingXingSelectedWithNum:(NSInteger)num{
    NSInteger tag = num - 1 + tagBase;
    for (UIButton * button in self.buttonArray) {
        if (button.tag <= tag) {
            button.selected = YES;
        }else{
            button.selected = NO;
        }
    }
    self.chosedNum = num + 1;
}
//传入几颗星星点亮 eg 4.5 则四颗半
- (void)stoneobs_fun_st_makeXingXingSelectedWithFloatNum:(CGFloat)num{
    NSInteger tag = num - 1 + tagBase;
    for (UIButton * button in self.buttonArray) {
        if (button.tag <= tag) {
            button.selected = YES;
        }else{
            button.selected = NO;
        }
    }
    self.chosedNum = num + 1;
    
    if (num - @(num).intValue > 0.1) {
        //后面那颗星星 变成半星
        NSInteger dealNum = @(num).intValue;
        if (num < 5) {
            UIButton * button = self.buttonArray[dealNum];
            [button setImage:[UIImage imageNamed:@"happy_game_index_stars_halfarge"] forState:UIControlStateNormal];
        }
    }
}
@end


